package com.example.sy2;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;

public class CountServlet extends HttpServlet {
    public void init() throws ServletException {
        int count = 0;
        ServletContext context = getServletContext();
        context.setAttribute("count", count);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        Integer count = (Integer) context.getAttribute("count");
        if (count == null) {
            count = 0;
        }
        count += 1;
        context.setAttribute("count", count);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>欢迎光临本网站! 这是您第" + count + "次访问</h1>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
